package org.fujene.structure;

import java.util.concurrent.CountDownLatch;

import org.fujene.communicate.hash.VirtualContentHash;

public class SearcherQueue{
	private TermList terms;
	private CountDownLatch latch;
	private ExtendTerm[] results;
	private boolean isNotFound;

	private SearcherQueue(){}

	public static SearcherQueue newSearcher(TermList terms, int maxSearcher){
		SearcherQueue returner = new SearcherQueue();

		returner.terms = terms;
		returner.latch = new CountDownLatch(terms.size() > maxSearcher ? maxSearcher : terms.size());
		returner.results = new ExtendTerm[terms.size() > maxSearcher ? maxSearcher : terms.size()];
		returner.isNotFound = false;

		return returner;
	}
	
	public long getTermHash(int index) {
		return VirtualContentHash.getTermHash(terms.get(index).str());
	}

	public int getCount(){
		return results.length;
	}

	public Term getTerm(int index){
		return terms.get(index);
	}

	public ExtendTerm getResult(int index){
		return results[index];
	}

	public void setResult(int index, ExtendTerm newResult){
		results[index] = newResult;
	}

	public void notFound(){
		isNotFound = true;
	}

	public boolean isNotFound(){
		return isNotFound;
	}

	public void countdown(){
		latch.countDown();
	}

	public void waitLatch(){
		try{
			latch.await();
		}catch(InterruptedException e){}
	}
}
